package selfview.sufutian.com.myapplication.draw2d;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by sufutian on 2018/10/11.
 */

public class TextReaderView extends View {

    TextPaint textPaint;

    private String TAG = "TextReaderView: ";


    private ArrayList<TEXTModel> textModels = new ArrayList<>();
    private StaticLayout staticLayout;


    private class TEXTModel {
        private String content;
        private long startTime;
        private long endTime;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public long getStartTime() {
            return startTime;
        }

        public void setStartTime(long startTime) {
            this.startTime = startTime;
        }

        public long getEndTime() {
            return endTime;
        }

        public void setEndTime(long endTime) {
            this.endTime = endTime;
        }
    }

    public TextReaderView(Context context) {
        super(context);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.RED);
        textPaint.setTextSize(50);

        for (int i = 0; i < 10; i++) {
            TEXTModel textModel = new TEXTModel();
            textModel.setContent("Canvas的绘图知识等，这篇文章也只是给" + i + "大家提个思路而已。如果有问题或者新,如果有问题或者新的想法欢迎交流:" + i);
            textModels.add(textModel);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


//        canvas.drawText(textModel.getContent(), 0, 100, textPaint);
//        for (int i = 0; i < textModels.size(); i++) {
//            TEXTModel textModel = textModels.get(i);
//            staticLayout = new StaticLayout(textModel.getContent(), textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
//            staticLayout.draw(canvas);
//        }


        TEXTModel textModel = textModels.get(0);
        staticLayout = new StaticLayout(textModel.getContent(), textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        staticLayout.draw(canvas);

        Log.e(TAG, "getBottomPadding: " + staticLayout.getBottomPadding());
        Log.e(TAG, "getEllipsizedWidth: " + staticLayout.getEllipsizedWidth());
        Log.e(TAG, "getLineCount: " + staticLayout.getLineCount());
        Log.e(TAG, "getHeight: " + staticLayout.getHeight());
        Log.e(TAG, "getWidth: " + staticLayout.getWidth());
        Log.e(TAG, "getEllipsizedWidth: " + staticLayout.getEllipsizedWidth());
        Log.e(TAG, "getLineWidth: " + staticLayout.getLineWidth(2));

        float measureText = textPaint.measureText(textModel.getContent());
        Log.e(TAG, "measureText: " + measureText);


        canvas.translate(staticLayout.getLineWidth(2), staticLayout.getHeight() - staticLayout.getHeight() / staticLayout.getLineCount());
        TEXTModel textModel_2 = textModels.get(1);
        staticLayout = new StaticLayout(textModel_2.getContent(), textPaint, (int) (canvas.getWidth() - staticLayout.getLineWidth(2)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        staticLayout.draw(canvas);


    }
}
